﻿using System;
using System.Windows;
using System.Windows.Controls;
using GameLogic.Controls;
using GameLogic.Logic;

namespace Dodger
{
    public partial class DodgerGame : UserControl, IGameFeatures
    {
        GameLoop gameLoop; // GameLoop behavior for collision and animation
      
        // IGameFeatures event handlers
        public event EventHandler<ScoresChangedEventArgs> ScoresChanged;
        public event EventHandler<LivesChangedEventArgs> LivesChanged;
        public event EventHandler<TimeChangedEventArgs> TimeChanged;
        public event EventHandler<GameOverEventArgs> GameOver;

        public DodgerGame()
        {
            InitializeComponent();

            this.Loaded += new RoutedEventHandler(OnLoaded);
        }

        void OnLoaded(object sender, RoutedEventArgs e)
        {
            // Find the GameLoop
            GameLoopFinder glf = new GameLoopFinder(LayoutRoot);
            gameLoop = glf.GameLoop;
        }

        public void Start()
        {
            // Show game ready screen
            VisualStateManager.GoToState(this, "GameReady", false);
        }

        public void Stop()
        {
            if (gameLoop != null)
            {
                gameLoop.ResetGameLoop();
            }
        }

        public void Pause()
        {
            if (gameLoop != null)
            {
                gameLoop.PauseGameLoop();
            }
        }

        public void Resume()
        {
            if (gameLoop != null)
            {
                gameLoop.StartGameLoop();
            
                // Show game screen
                VisualStateManager.GoToState(this, "Playing", false);
            }
        }

        public void OnScoresChanged(int p)
        {
            if (ScoresChanged != null)
            {
                ScoresChangedEventArgs args = new ScoresChangedEventArgs();
                args.Scores = p;
                ScoresChanged(this, args);
            }
        }


        public void OnLivesChanged(int lives)
        {
            if (lives < 0)
            {
                // Reset game
                this.Stop();

                // Show continue screen
                VisualStateManager.GoToState(this, "LifeLost", false);
            }
           
            // Tell the game container to change lives
            if (LivesChanged != null)
            {
                LivesChangedEventArgs args = new LivesChangedEventArgs();
                args.Lives = lives;
                LivesChanged(this, args);
            }
        }

        private void _continue_Click(object sender, RoutedEventArgs e)
        {
            // Reset game
            this.Stop();

            // Continue game
            this.Resume();
        }

        private void _startGame_Click(object sender, RoutedEventArgs e)
        {
            // Reset game
            this.Stop();

            // Start game loop
            this.Resume();
        }

        #region IGameFeatures
        public bool IsTimeBased
        {
            get { return false; }
        }

        public bool IsLifeBased
        {
            get { return true; }
        }

        public string GameName
        {
            get { return "Dodger"; }
        }


        public TimeSpan GameDuration
        {
            get { return new TimeSpan(); }
        }

        public string DeveloperName
        {
            get { return "Dennis Sasse"; }
        }

        public string CompanyName
        {
            get { return "Microsoft Deutschland GmbH"; }
        }

        public string Description
        {
            get { return "Evade the moving black boxes as long as you can!"; }
        }

        public Uri SettingsPage
        {
            get { return null; }
        }
        #endregion


    }
}
